package mat;
import types.*;
import javax.swing.*;
import java.awt.*;

public class MCanvas extends JFrame{
	/**
	 * Layout management
	 */
	private static final long serialVersionUID = 1L;
	
	public MCanvas(){
		/*Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		screenHeight = screenSize.getHeight() / 3 * 2;
		screenWidth = screenSize.getWidth() / 3 * 2;
		*/
		windowHeight = 800; 
		windowWidth = 800;
		
		setSize((int)windowWidth, (int)windowHeight);
		setTitle("MAT-Mobile Authoring Tool DEMO PHONE");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public MCanvas(double x, double y){ // Not correct 
		// get default screen dimensions
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		windowHeight = screenSize.getHeight();
		windowWidth = screenSize.getWidth();
		System.out.println(windowHeight+","+windowWidth);

		// center frame in center, and assign title
		if ( x > windowWidth) x = windowWidth;
		if ( y > windowHeight) y = windowHeight;

		Double x1 = new Double(x);
		Double y1 = new Double(y);
		//setSize(x1.intValue(), y1.intValue());
		setSize((int)windowWidth, (int)windowHeight);
		x1 = windowWidth / 4;
		y1 = windowHeight / 4;
		//setLocation(x1.intValue(), y1.intValue());
		setTitle("MAT DEMO");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void addLabel(MLabel ml){
		mlp.pushNew(ml);
	}
	
	public void addPic(MPic mp){
		mpp.pushNew(mp);
	}
	
	public void showResult(){
		//getContentPane().setLayout(new BorderLayout());
        /* Use an appropriate Look and Feel */
		/*
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        // Turn off metal's use bold fonts 
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        */
		//getContentPane().setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
		
		ImagePanel page_start = new ImagePanel(new ImageIcon(".\\iphone_head.png").getImage());
		page_start.setPreferredSize(new Dimension(800, 172));
		getContentPane().add(page_start, BorderLayout.PAGE_START);
		
		ImagePanel page_left = new ImagePanel(new ImageIcon(".\\iphone_left.png").getImage());
		page_left.setPreferredSize(new Dimension(75, 493));
		getContentPane().add(page_left, BorderLayout.LINE_START);
		
		ImagePanel content = new ImagePanel(new ImageIcon(".\\iphone_center.png").getImage());
		LayoutManager overlay = new OverlayLayout(content);
		content.setLayout(overlay);
		content.setPreferredSize(new Dimension(651, 493)); // 651 493
		content.add(mpp);
		content.add(mlp);
		/*
		System.out.println(" mpp alignmentX: "+mpp.getAlignmentX()+"  alignmentY: "+mpp.getAlignmentY());
		getContentPane().add(mpp, BorderLayout.CENTER);
		System.out.println(" mlp alignmentX: "+mlp.getAlignmentX()+"  alignmentY: "+mlp.getAlignmentY());
	    getContentPane().add(mlp, BorderLayout.CENTER);
	    */
	    getContentPane().add(content, BorderLayout.CENTER);
	    
	     
		ImagePanel page_right = new ImagePanel(new ImageIcon(".\\iphone_right.png").getImage());
		page_right.setPreferredSize(new Dimension(60, 493));
		getContentPane().add(page_right, BorderLayout.LINE_END);
		
		ImagePanel page_tail = new ImagePanel(new ImageIcon(".\\iphone_tail.png").getImage());
		page_tail.setPreferredSize(new Dimension(800, 135));
		getContentPane().add(page_tail, BorderLayout.PAGE_END);

		setSize((int)windowWidth, (int)windowHeight);
		//pack()
		setVisible(true);
	}	
	
	public void showPic(){
		add(mpp);
		validate();
	}
	
	public void showLabel(){
		add(mlp);
		validate();
	}
	
	private class ImagePanel extends JPanel {
		 /**
		 * for displaying the picture component
		 */
		private static final long serialVersionUID = 1L;
		private Image img;
		  public ImagePanel(String img) {
		    this(new ImageIcon(img).getImage());
		  }

		  public ImagePanel(Image img) {
		    this.img = img;
		    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));		    
		    setPreferredSize(size);
		    setMinimumSize(size);
		    setMaximumSize(size);
		    setSize(size);
		    setLayout(null);
		  }

		  public void paintComponent(Graphics g) {
		    g.drawImage(img, 0, 0, null);
		  }
	}

	// the overall window size
	private static double windowHeight;
	private static double windowWidth;
	/*
	public static int PhoneScreenHeight = Mat.get_DocHeight();
	public static int PhoneScreenWidth = Mat.get_DocWidth();
	public static int PhoneHeight = PhoneScreenHeight+ 200;
	public static int PhoneWidth = PhoneScreenWidth + 20;
	*/
	// the content(cell phone screen) size (651, 493)
	public static int PhoneScreenHeight = (int) (493);
	public static int PhoneScreenWidth = (int) (651);
	
	private MLabelPanel mlp = new MLabelPanel();
	private MPicPanel mpp = new MPicPanel();
}


